<?php
    //上传配置
    $config = array(
        "uploadPath"=>"upload/",                          //保存路径
        "fileType"=>array(".gif",".png",".jpg",".jpeg",".bmp"),   //文件允许格式
        "fileSize"=>1000                                          //文件大小限制，单位KB
    );
    //原始文件名，表单名固定，不可配置
    $oriName = htmlspecialchars($_POST['fileName'], ENT_QUOTES);

    //上传图片框中的描述表单名称，
    $title = htmlspecialchars($_POST['pictitle'], ENT_QUOTES);

    //文件句柄
    $file = $_FILES["upfile"];

    //文件上传状态,当成功时返回SUCCESS，其余值将直接返回对应字符窜并显示在图片预览框，同时可以在前端页面通过回调函数获取对应字符窜
    $state = "SUCCESS";

    //重命名后的文件名
    $fileName="";

    //格式验证
    $current_type = strtolower(strrchr($file["name"], '.'));
    if(!in_array($current_type, $config['fileType'])){
        $state = $current_type;
    }
    //大小验证
    $file_size = 1024 * $config['fileSize'];
    if( $file["size"] > $file_size ){
        $state = "b";
    }
    //保存图片
    if($state == "SUCCESS"){
//        $tmp_file=$file["name"];
//        $filename = time().strrchr($file["name"],'.');
//        $bucket = 'baeueditor';
//        $opt = array('acl'=>BaiduBCS::BCS_SDK_ACL_TYPE_PUBLIC_READ);
//        $baidu_bcs = new BaiduBCS();
//
//
//        $object =  "/$filename";
//        $fileUpload = $file["tmp_name"];
//
//        if( !file_exists($fileUpload) )
//        {
//            echo "{'url':'".$fileName."','original':'".$file['name']."','state':'上传失败'}";
//        }
//
//        $baidu_bcs->create_object ( $bucket, $object, $fileUpload, $opt );
//        $url = $baidu_bcs->generate_get_object_url($bucket, $object);
        //error_reporting(E_ERROR | E_WARNING | E_PARSE);
        error_reporting(0);
//        require_once('../../../../../wp-load.php');
//        require_once('../../../../../wp-admin/includes/file.php');
//        require_once('../../../../../wp-admin/includes/image.php');
//        $_POST['action'] = 'wp_handle_upload';
//        $file = wp_handle_upload($file, 'false');
//
//        if ( isset($file['error']) )
//            wp_die( $file['error'] );
//
//        $url = $file['url'];
//        $type = $file['type'];
//        $file = $file['file'];
//        $filename = basename($file);
//
//        $post_id = $_REQUEST['post_id'];
//        // Construct the object array
//        $attachment = array_merge( array(
//            'post_mime_type' => $type,
//            'guid' => $url,
//            'post_parent' => $post_id,
//            'post_title' => $title,
//            'post_content' => '',
//        ), array() );
//
//        // Save the data
//        $id = wp_insert_attachment($attachment, $file, $post_id);
//        wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );

        require_once('../../../../../wp-load.php');
        require_once('../../../../../wp-admin/includes/file.php');
        require_once('../../../../../wp-admin/includes/post.php');
        require_once('../../../../../wp-admin/includes/image.php');
        require_once('../../../../../wp-admin/includes/media.php');
        require_once('../../../../../wp-includes/post.php');
        $rsid = media_handle_upload("upfile", $_REQUEST['post_id']);
        $url = wp_get_attachment_url($rsid);
    }
    //向浏览器返回数据json数据
    /**
     * 返回数据格式
     * {
     *   'url'      :'a.jpg',   //保存后的文件路径
     *   'title'    :'hello',   //文件描述，对图片来说在前端会添加到title属性上
     *   'original' :'b.jpg',   //原始文件名
     *   'state'    :'SUCCESS'  //上传状态，成功时返回SUCCESS,其他任何值将原样返回至图片上传框中
     * }
     */
//    echo "{'url':'".$url."','original':'".$file['name']."','state':'$state'}";
echo "{'url':'".$url."','original':'".$_FILES["upfile"]['name']."','state':'$state'}";
?>

